#!/usr/bin/env python
# coding=utf-8
# __author__ = 'Yunchao Ling'

from pymongo import MongoClient


def ValidateRelation():
    ht = {
        "PMapp_Gene": "PMapp_Gene_20190724",
        "PMapp_Disease": "PMapp_Disease_20190724",
        "PMapp_Drug": "PMapp_Drug_20190612",
        "PMapp_Pathway": "PMapp_Pathway_20190725",
        "PMapp_Protein": "PMapp_Protein_20190612",
        "PMapp_Variation": "PMapp_Variation_20190726",
        "PMapp_codingRNA": "PMapp_codingRNA_20190611",
        "PMapp_lncRNA": "PMapp_lncRNA_20190612",
        "PMapp_miRNA": "PMapp_miRNA_20190611"
    }

    client = MongoClient("10.188.188.104", 27104)
    db = client["PMapp"]
    db.authenticate("pmapp", "pmapp")
    collection = db["Relation_20190725"]
    ht_collection = {}
    for key in ht.keys():
        ht_collection[key] = db[ht[key]]

    fail = 0
    count=0
    for result in collection.find({}, {"entity_set": 1}):
        for item in result["entity_set"]:
            if ht_collection[item["db"]].find_one({"dms_id": item["id"]}) == None:
                # fail +=1
                collection.remove({"_id":result["id"]})
                break

    client.close()
    print(fail)


if __name__ == '__main__':
    ValidateRelation()
